#1、获取“https://www.pxc.jx.cn/”请求
#2、拿去该网站的文本
#3、将获取的文本保存在D:\\萍乡学院.html文件中

import requests
import os

def get_url(url):
    """获取url"""
    try:
        response = requests.get(url)
        #response.raise_for_status()  # 检查请求是否成功
        response.encoding = response.apparent_encoding  # 自动检测并设置正确编码
        return response.text
    except requests.RequestException as e:
        print(f"Error fetching URL: {e}")
        return None

def save_file(content, file_path):
    """保存文件"""
    try:
        with open(file_path, 'w', encoding='utf-8') as file:
            file.write(content)         # 写入文件内容
        print(f"File saved successfully at: {file_path}")   
    except Exception as e:
        print(f"Error saving file: {e}")

# if __name__ == "__main__":
#     try:
#         # 这里应该有具体的表达式，由于不清楚具体需求，这里留空示例，可根据实际情况补充
#         pass  
#     except Exception as e:
#         print(f"Error saving file: {e}")

if __name__ == "__main__":
    url = "https://www.pxc.jx.cn/"
    content = get_url(url)  # 获取网页内容
    if content:
        file_path = r"D:\\萍乡学院.html"  # 文件保存路径
        save_file(content, file_path)  # 保存文件
        

